package com.sunbeam.common.utils;

import java.util.ArrayList;
import java.io.File;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import com.sunbeam.common.consts.EConstants;
import com.sunbeam.common.system.ParamObj.CodeNameInfo;

/**
 * @Title: XmlUtils.java
 * @Package com.sunbeam.common.utils
 * @Description: TODO
 * @author lyz lyzpoq_gmail_com
 * @date 2013/10/31 11:36:35
 * @version V1.0
 */
public class XmlUtils {

	/**
	 * @Title: main
	 * @Description: TODO
	 * @param @param args
	 * @return void
	 * @throws
	 */

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//System.out.println(getMiscCodeNameInfos("GLCODE").size());
		System.out.println(getMiscDetailName("DECRYPT_ENABLED","DES"));
		
		for(CodeNameInfo codeNameInfo:getMiscCodeNameInfos("DECRYPT_ENABLED")){
			System.out.println("Code: "+codeNameInfo.getCode()+" Name: "+codeNameInfo.getName());
		}
		// System.exit (0);

	}// end of main

	/**
	 * 
	 * @Title: getMiscCodeNameInfos
	 * @Description: TODO
	 * @param @param searchId
	 * @param @return
	 * @return ArrayList<CodeNameInfo>
	 * @throws
	 */
	@SuppressWarnings("unchecked")
	public static ArrayList<CodeNameInfo> getMiscCodeNameInfos(String miscCode) {
		if(StringUtils.isNullOrEmpty(miscCode)){
			return null;
		}
		return (ArrayList<CodeNameInfo>)getMiscInfoCommon(miscCode, null);
	}

	/**
	 * 
	 * @Title: getMiscDetailName
	 * @Description: TODO
	 * @param @param searchCode
	 * @param @param code
	 * @param @return
	 * @return String
	 * @throws
	 */
	public static String getMiscDetailName(String miscCode, String miscDetailCode){
		if(StringUtils.isNullOrEmpty(miscCode)||StringUtils.isNullOrEmpty(miscDetailCode)){
			return null;
		}
		return (String)getMiscInfoCommon(miscCode, miscDetailCode);	
	}
	
	
	/**
	 * 
	 * @Title: getMiscInfoCommon
	 * @Description: TODO
	 * @param @param searchCode
	 * @param @param code
	 * @param @return
	 * @return Object
	 * @throws
	 */
	public static Object getMiscInfoCommon(String miscCode, String miscDetailCode) {
		ArrayList<CodeNameInfo> codeNameInfos = new ArrayList<CodeNameInfo>();
		String miscDetailName=StringUtils.Empty;
		try {
			String miscCodeConfigFileName = StringUtils
					.URLPathCombine(new String[] {
							XmlUtils.class.getClassLoader().getResource("")
									.getPath(),
							EConstants.DEFAULT_SETTINGS_DIRECTORY,
							"miscCodeMapping.xml" });

			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(new File(miscCodeConfigFileName));

			// normalize text representation
			doc.getDocumentElement().normalize();

			NodeList listOfPersons = doc.getElementsByTagName("Misc");

			for (int i = 0; i < listOfPersons.getLength(); i++) {
				Node miscDetailNode = listOfPersons.item(i);
				Element miscElement = (Element) miscDetailNode;
				if (miscDetailNode.getNodeType() == Node.ELEMENT_NODE
					&& miscCode.equals(miscElement.getAttribute("code"))) {
					NodeList miscElementList = miscElement.getElementsByTagName("MiscDetail");
					for (int j = 0; j < miscElementList.getLength(); j++) {
						Element miscDetailElement = (Element) miscElementList.item(j);						
						if ((!StringUtils.isNullOrEmpty(miscDetailCode))
							&& miscDetailCode.equals(miscDetailElement.getAttribute("code"))) {
							miscDetailName = miscDetailElement.getAttribute("name");
							break;
						}
						CodeNameInfo codeNameInfo = new CodeNameInfo();
						codeNameInfo.setCode(miscDetailElement.getAttribute("code"));
						codeNameInfo.setName(miscDetailElement.getAttribute("name"));
						codeNameInfos.add(codeNameInfo);
					}				
				}
			}
		} catch (SAXParseException err) {
			System.out.println("** Parsing error" + ", line "
					+ err.getLineNumber() + ", uri " + err.getSystemId());
			System.out.println(" " + err.getMessage());

		} catch (SAXException e) {
			Exception x = e.getException();
			((x == null) ? e : x).printStackTrace();

		} catch (Throwable t) {
			t.printStackTrace();
		}
		if(StringUtils.isNullOrEmpty(miscDetailCode)){
			return codeNameInfos;
		}else{
			return miscDetailName;
		}
	}

}
